//创建服务器
const express = require('express')
// 第二步:通过express()在内存中创建一个服务器对象，我们都通过使用该对象完成服务器一系列操作。
const app = express()
const Router = require("./router/user")
const Adopt = require("./router/adopt")
const Text = require("./router/text")
const Petmessage = require("./router/petmessage")
const myPet = require('./router/mypet')
const Apply = require('./router/apply')
const Admin = require('./router/gly')
const User = require('./router/yh')
const Cwxx = require('./router/cwxx')
const Cwlyxx = require('./router/cwlyxx')
const video = require('./router/video')



// 第三步:通过服务器对象的listen()启动服务器
app.listen(3000, () => {
  console.log('服务器启动成功！')
})

// 加载CORS模块 跨域
const cors = require('cors');

// 使用CORS中间件
app.use(cors({
origin: ['http://localhost:8080', 'http://127.0.0.1:8080','http://127.0.0.1:8081','http://localhost:8081']
})); 


app.use(express.urlencoded({
  extended: true
}));

app.use(express.json());

app.use('/user', Router)
app.use('/adopt', Adopt)
app.use('/', Text)
app.use('/petmessage', Petmessage)
app.use('/myPet', myPet)
app.use('/Apply', Apply)
app.use('/', Admin)
app.use('/', User)
app.use('/', Cwxx)
app.use('/', Cwlyxx)
app.use('/video', video)



// 全局错误处理中间件：
app.use((err, req, res, next) => {
  console.log(err);
  res.send({code:404,msg:'系统内部错误，请稍后重试！'})
});


// 静态资源托管目录
app.use(express.static('upload'));